﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Threading;
using ggprot;

namespace GGTest
{
    public partial class Chat : Form
    {
        private int uin;
        private GaduProtocol protocol;
        private bool typing = false;
        private ManualResetEvent MRE;

        public Chat(int uin, GaduProtocol protocol)
        {
            InitializeComponent();

            webBrowser1.DocumentCompleted += new WebBrowserDocumentCompletedEventHandler(webBrowser1_DocumentCompleted);
            webBrowser1.DocumentText = "<html><body></body></html>";

            this.uin = uin;
            this.Text = string.Format("Chat - {0}", uin);

            this.protocol = protocol;

            protocol.MessageReceived += new MessageReceivedEventHandler(protocol_MessageReceived);
            protocol.TypingNotifyReceived += new TypingNotifyEventHandler(protocol_TypingNotifyReceived);
            MRE = new ManualResetEvent(false);
        }

        void webBrowser1_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
        {
            MRE.Set();
        }

        void protocol_TypingNotifyReceived(object sender, TypingNotifyEventArgs e)
        {
            if (e.Uin == uin)
            {
                if (e.Typing)
                    statusStrip1.Items.Add(string.Format("{0} pisze wiadomość...", uin)).Name = "typing";
                else
                    statusStrip1.Items.RemoveByKey("typing");
            }
        }

        public int Uin
        {
            get { return uin; }
        }

        void protocol_MessageReceived(object sender, MessageReceivedEventArgs e)
        {
            if (webBrowser1.InvokeRequired)
                webBrowser1.Invoke(new Action(delegate()
                {
                    webBrowser1.Document.Body.InnerHtml += string.Format("{2}{0} pisze:<br />{1}", e.Uin, e.Message, string.IsNullOrEmpty(webBrowser1.Document.Body.InnerHtml) ? string.Empty : "<br /><br />");
                }));
        }

        private void richTextBox2_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar == (char)Keys.Enter)
            {
                protocol.SendMessage(richTextBox2.Text, uin);
                webBrowser1.Document.Body.InnerHtml += string.Format("{1}Napisałeś:<br />{0}", richTextBox2.Text, string.IsNullOrEmpty(webBrowser1.Document.Body.InnerHtml) ? string.Empty : "<br /><br />");
                richTextBox2.Clear();
            }
        }

        public void AppendMessage(int uin, string message)
        {
            Thread thr = new Thread(delegate()
                {
                    Action act = new Action(delegate()
                    {
                        webBrowser1.Document.Body.InnerHtml += string.Format("{2}{0} pisze:<br />{1}", uin, message, string.IsNullOrEmpty(webBrowser1.Document.Body.InnerHtml) ? string.Empty : "<br /><br />");
                    });

                    MRE.WaitOne();
                    if (webBrowser1.InvokeRequired)
                        webBrowser1.Invoke(act);
                    else
                        act();
                });
            thr.Start();
        }

        private void richTextBox2_TextChanged(object sender, EventArgs e)
        {
            if (typing == false && richTextBox2.Text.Length > 0)
                protocol.TypingNotify(uin, true);
            else if (typing == true && richTextBox2.Text.Length == 0)
                protocol.TypingNotify(uin, false);

            typing = richTextBox2.Text.Length > 0;
        }
    }
}
